package com.zlm.app.emp.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zlm.common.util.CheckParamsUtils;
import com.zlm.common.dto.userEmp.*;
import com.zlm.app.emp.service.UserEmpService;
import com.zlm.common.constants.CodeConstants;
import com.zlm.common.dto.JsonResultDto;
import com.zlm.common.entity.EmpUserEducationDO;
import com.zlm.common.entity.EmpUserExperienceDO;
import com.zlm.common.entity.SysFeedbackDO;
import com.zlm.common.entity.UserTokenDO;
import com.zlm.common.util.CardUtil;
import com.zlm.common.util.DateTimeUtils;
import com.zlm.common.util.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 我的-页面
 * @Author: duyufu
 * @Date: 2019/5/10 15:13
 */
@RestController
@RequestMapping(value = "/myPage")
public class MyPageController extends CheckController {

    @Autowired
    private UserEmpService userEmpService;
    /**
     * 获取用户个人信息
     * @param token
     * @return
     */
    @GetMapping("/getEmpInfo")
    public JsonResultDto getEmpInfo(String token) {
        try {
            //判断token失效
            UserTokenDO userToken = checkToken(token);
            //查询用户信息
            JsonResultDto result = userEmpService.getEmpInfo(userToken.getUserId());
            checkJsonResultDto(result);
            return success(result.getData());
        } catch (RuntimeException e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 保存个人基本信息
     * @param empInfoParamsDto
     * @return
     */
    @PostMapping("/saveEmpInfo")
    public JsonResultDto saveEmpInfo(EmpInfoParamsDto empInfoParamsDto) {
        try {
            //检查必要参数
            if (!CheckParamsUtils.checkEmpInfo(empInfoParamsDto)) {
                throw new RuntimeException(CodeConstants.CODE_PARAMS);
            }
            if (DateTimeUtils.differentDaysByMillisecond(DateTimeUtils.String2Date(empInfoParamsDto.getBirthdayString()) ,
                    new Date())<0) {
                throw new RuntimeException(CodeConstants.CODE_10015);
            }
            UserTokenDO userToken = checkToken(empInfoParamsDto.getToken());
            empInfoParamsDto.setEmpId(userToken.getUserId());
            //保存用户信息
            JsonResultDto result = userEmpService.saveEmpInfo(empInfoParamsDto);
            checkJsonResultDto(result);
            return success();
        } catch (RuntimeException e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }

    }

    /**
     * 获取用户简历
     * @param token
     * @return
     */
    @GetMapping("/getEmpResume")
    public JsonResultDto getEmpResume(String token) {
        try {
            UserTokenDO userToken = checkToken(token);
            JsonResultDto result = userEmpService.getEmpResume(userToken.getUserId());
            checkJsonResultDto(result);
            return success(result.getData());
        } catch (RuntimeException e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 获取用户求职意向
     * @param token
     * @return
     */
    @GetMapping("/getEmpPostResume")
    public JsonResultDto getEmpPostResume(String token) {
        try {
            UserTokenDO userToken = checkToken(token);
            JsonResultDto result = userEmpService.getEmpPostResume(userToken.getUserId());
            checkJsonResultDto(result);
            return success(result.getData());
        } catch (RuntimeException e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 添加/修改用户工作经历
     * @param empUserExperienceDO
     * @return
     */
    @PostMapping("/saveUserExperience")
    public JsonResultDto saveUserExperience(EmpUserExperienceDO empUserExperienceDO) {
        try {
            //检查必要参数
            if (!CheckParamsUtils.checkExperience(empUserExperienceDO)) {
                throw new RuntimeException(CodeConstants.CODE_PARAMS);
            }
            if (DateTimeUtils.differentDaysByMillisecond(empUserExperienceDO.getStartDate(),
                    empUserExperienceDO.getEndDate())<0) {
                throw new RuntimeException(CodeConstants.CODE_10016);
            }
            UserTokenDO userToken = checkToken(empUserExperienceDO.getToken());
            //保存用户工作经历
            empUserExperienceDO.setEmpId(userToken.getUserId());
            JsonResultDto result = userEmpService.saveUserExperience(empUserExperienceDO);
            checkJsonResultDto(result);
            return success();
        } catch (RuntimeException e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 删除用户工作经历
     * @param token
     * @param eid
     * @return
     */
    @GetMapping("/deleteUserExperience")
    public JsonResultDto deleteUserExperience(String token, Integer eid) {
        try {
            if (null == eid) {
                throw new RuntimeException(CodeConstants.CODE_PARAMS);
            }
            //判断token失效
            UserTokenDO userToken = checkToken(token);
            JsonResultDto result = userEmpService.deleteUserExperience(userToken.getUserId(),eid);
            checkJsonResultDto(result);
            return success();
        } catch (RuntimeException e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 添加/修改用户教育经历
     * @param empUserEducationDO
     * @return
     */
    @PostMapping("/saveUserEducation")
    public JsonResultDto saveUserEducation(EmpUserEducationDO empUserEducationDO) {
        try {
            //检查必要参数
            if (!CheckParamsUtils.checkEducation(empUserEducationDO)) {
                throw new RuntimeException(CodeConstants.CODE_PARAMS);
            }
            if (DateTimeUtils.differentDaysByMillisecond(empUserEducationDO.getStartDate(),
                    empUserEducationDO.getEndDate())<0) {
                throw new RuntimeException(CodeConstants.CODE_10016);
            }
            //判断token失效
            UserTokenDO userToken = checkToken(empUserEducationDO.getToken());
            //保存用户教育经历
            empUserEducationDO.setEmpId(userToken.getUserId());
            JsonResultDto result = userEmpService.saveUserEducation(empUserEducationDO);
            checkJsonResultDto(result);
            return success();
        } catch (RuntimeException e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 删除用户教育经历
     * @param token
     * @param eid
     * @return
     */
    @GetMapping("/deleteUserEducation")
    public JsonResultDto deleteUserEducation(String token, Integer eid) {
        try {
            if (null == eid) {
                throw new RuntimeException(CodeConstants.CODE_PARAMS);
            }
            //判断token失效
            UserTokenDO userToken = checkToken(token);
            JsonResultDto result = userEmpService.deleteUserEducation(userToken.getUserId(),eid);
            checkJsonResultDto(result);
            return success();
        } catch (RuntimeException e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 查询全部岗位菜单
     * @param category
     * @return
     */
    @GetMapping("/queryPostMenu")
    public JsonResultDto queryPostMenu(Integer category) {
        try {
            if (null == category) {
                throw new RuntimeException(CodeConstants.CODE_PARAMS);
            }
            JsonResultDto result = userEmpService.getAllPostMenu(category);
            checkJsonResultDto(result);
            return success(result.getData());
        } catch (RuntimeException e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 保存用户需求
     * @param empResumeParamsDto
     * @return
     */
    @PostMapping("/saveEmpResume")
    public JsonResultDto saveEmpResume( EmpResumeParamsDto empResumeParamsDto) {
        try {
            //检查必要参数
            if (!CheckParamsUtils.checkResume(empResumeParamsDto)) {
                throw new RuntimeException(CodeConstants.CODE_PARAMS);
            }
            //判断token失效
            UserTokenDO userToken = checkToken(empResumeParamsDto.getToken());
            empResumeParamsDto.setEmpId(userToken.getUserId());
            List<Integer> industryList = empResumeParamsDto.getIndustryList();
            // 数据库表结构设计错误，以后修改emp_resume表结构注意留意此段代码修改
            if (null != industryList) {
                if (industryList.size() > 0) {
                    empResumeParamsDto.setIndustry(industryList.get(0));
                }
                if (industryList.size() > 1) {
                    empResumeParamsDto.setIndustry1(industryList.get(1));
                }
                if (industryList.size() > 2) {
                    empResumeParamsDto.setIndustry2(industryList.get(2));
                }
            }
            JsonResultDto result = userEmpService.saveEmpResume(empResumeParamsDto);
            checkJsonResultDto(result);
            return success();
        } catch (RuntimeException e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 获取用户收藏的公司列表
     * @param orgCollectParamsDto
     * @return
     */
    @PostMapping("/queryEmpOrgCollect")
    public JsonResultDto queryEmpOrgCollect(OrgCollectParamsDto orgCollectParamsDto) {
        try {
            //判断token失效
            UserTokenDO userToken = checkToken(orgCollectParamsDto.getToken());
            orgCollectParamsDto.setEmpId(userToken.getUserId());
            JsonResultDto result = userEmpService.queryEmpOrgCollect(orgCollectParamsDto);
            checkJsonResultDto(result);
            return success(result.getData());
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 获取用户收藏的职位列表
     * @param positionCollectParamsDto
     * @return
     */
    @PostMapping("/queryEmpPostCollect")
    public JsonResultDto queryEmpPostCollect(PositionCollectParamsDto positionCollectParamsDto) {
        try {
            //判断token失效
            UserTokenDO userToken = checkToken(positionCollectParamsDto.getToken());
            positionCollectParamsDto.setEmpId(userToken.getUserId());
            JsonResultDto result = userEmpService.queryEmpPostCollect(positionCollectParamsDto);
            checkJsonResultDto(result);
            return success(result.getData());
        } catch (RuntimeException e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 实名认证
     * @param token
     * @param realName
     * @param idNo
     * @param idcardFront
     * @param idcardBehind
     * @return
     */
    @GetMapping("/realNameAuthentication")
    public JsonResultDto realNameAuthentication(String token,String realName,String idNo,String idcardFront,
                                                String idcardBehind) {
        try {
            if (!CheckParamsUtils.checkRealName(realName,idNo,idcardFront,idcardBehind)) {
                throw new RuntimeException(CodeConstants.CODE_PARAMS);
            }
            Map<String, Object> empInfo = CardUtil.getCardInfo(idNo);
            if (empInfo == null) {
                throw new RuntimeException(CodeConstants.CODE_10013);
            }
            //判断token失效
            UserTokenDO userToken = checkToken(token);
            empInfo.put("empId",userToken.getUserId());
            empInfo.put("realName",realName);
            empInfo.put("idNo",idNo);
            empInfo.put("idcardFront",idcardFront);
            empInfo.put("idcardBehind",idcardBehind);

            JsonResultDto result = userEmpService.realNameAuthentication(empInfo);
            checkJsonResultDto(result);
            return success(result.getData());
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 意见反馈
     * @param token
     * @param mobile
     * @param content
     * @return
     */
    @GetMapping("/saveMemAgencyFavourite")
    public JsonResultDto saveMemAgencyFavourite(String token,String mobile,String content) {
        try {
            //判断token失效
            UserTokenDO userToken = checkToken(token);
            SysFeedbackDO feedback = new SysFeedbackDO();
            feedback.setEmpId(userToken.getUserId());
            feedback.setContent(content);
            JsonResultDto result = userEmpService.saveMemAgencyFavourite(feedback);
            checkJsonResultDto(result);
            return success();
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 保存体检报告
     * @param token
     * @param request
     * @param fileType
     * @return
     */
    @PostMapping("/saveHealthReport")
    public JsonResultDto saveHealthReport(String token, HttpServletRequest request, String fileType) {
        try {
            //判断token失效
            UserTokenDO userToken = checkToken(token);
            List<String> files = FileUtils.fileUpload(request, fileType);
            String[] urlList=files.toArray(new String[files.size()]);

            JsonResultDto result = userEmpService.saveHealthReport(userToken.getUserId(),urlList);
            checkJsonResultDto(result);
            return success();
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 删除体检报告
     * @param token
     * @param attId
     * @return
     */
    @GetMapping("/deleteHealthReport")
    public JsonResultDto deleteHealthReport(String token, Integer attId) {
        try {
            //判断token失效
            UserTokenDO userToken = checkToken(token);
            JsonResultDto result = userEmpService.deleteHealthReport(userToken.getUserId(),attId);
            checkJsonResultDto(result);
            return success();
        } catch (RuntimeException e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 查询近一年的体检报告
     * @param token
     * @return
     */
    @GetMapping("/queryHealthReport")
    public JsonResultDto queryHealthReport(String token) {
        try {
            //判断token失效
            UserTokenDO userToken = checkToken(token);
            JsonResultDto result = userEmpService.queryHealthReport(userToken.getUserId());
            checkJsonResultDto(result);
            return success(result.getData());
        } catch (RuntimeException e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

}
